package me.youline.dataServer.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.transaction.Transactional;

import me.youline.dataServer.entity.UserRelationRecorder;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

@Component
public class UserSocialInfoRecorderDAO {

	private static final String TABLE_NAME = "yline_social_user_records";

	private static final String INSERT_SQL = "insert into " + TABLE_NAME
			+ " (userId,followersNum,followingNum,isRecommend) values(?,?,?,?)";

	private static final String ADD_FOLLOWERS_NUM_SQL = "update " + TABLE_NAME
			+ " set followersNum = followersNum+? where userId = ?";

	private static final String ADD_FOLLOWING_NUM_SQL = "update " + TABLE_NAME
			+ " set followingNum = followingNum+? where userId = ?";
	
	private static final String QUERY_RECOMMEND_USER_LIST_SQL = "select userId from "+TABLE_NAME+ " where isRecommend = ?"; 
	
	private static final String QUERY_REL_TOTAL_DATA = " select * from "+TABLE_NAME+ " where userId = ?";

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	private static RowMapper<UserRelationRecorder> row = new RowMapper<UserRelationRecorder>() {
		
		@Override
		public UserRelationRecorder mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			UserRelationRecorder t = new UserRelationRecorder();
			t.setFollowersTotal(rs.getInt("followersNum"));
			t.setFollowingTotal(rs.getInt("followingNum"));
			t.setUserId(rs.getLong("userId"));
			return t;
		}
	};
	
	/**
	 * 添加用户关系统计实体
	 * @param t
	 */
	@Transactional
	public void addUserRelationTotal(UserRelationRecorder t) {
		jdbcTemplate.update(
				INSERT_SQL,
				new Object[] { t.getUserId(), t.getFollowersTotal(),
						t.getFollowingTotal(),t.isRecommend() });
	}

	/**
	 * 更新用户粉丝数
	 * @param userId
	 * @param num
	 */
	@Transactional
	public void addFollowersNum(Long userId, int num) {
		jdbcTemplate
				.update(ADD_FOLLOWERS_NUM_SQL, new Object[] { num, userId });
	}

	/**
	 * 更新关注数
	 * @param userId
	 * @param num
	 */
	@Transactional
	public void addFollowingNum(Long userId, int num) {
		jdbcTemplate
				.update(ADD_FOLLOWING_NUM_SQL, new Object[] { num, userId });
	}
	
	/**
	 * 根据用户ID获取关系统计信息数据
	 * @param userId
	 * @return
	 */
	public UserRelationRecorder getRelationTotalByUser(Long userId){
		UserRelationRecorder total;
		try {
			total = jdbcTemplate.queryForObject(QUERY_REL_TOTAL_DATA, new Object[]{userId},row);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
		return total;
	}
	
	/**
	 * 获取受推荐用户的列表
	 * @return
	 */
	public List<Long> getRecommendUserIds(){
		return jdbcTemplate.queryForList(QUERY_RECOMMEND_USER_LIST_SQL, Long.class, new Object[]{1});
	}
}
